//
//  PickerView.swift
//  手账
//
//  Created by zhuxiaoxiang on 2018/12/16.
//  Copyright © 2018年 zhuxiaoxiang. All rights reserved.
//

import UIKit

class PickerView: UIView {
    
    typealias SelectBlock = ((_ text: String?) -> ())

    var _data: Array<String>? {
        didSet {
            self._pickerView.reloadAllComponents()
        }
    }
    var _toolBar: UIToolbar!
    var _pickerView: UIPickerView!
    var _block: SelectBlock?
    
    init(frame: CGRect, data: Array<String>) {
        super.init(frame: frame)
        self._data = data
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PickerView {
    func setupUI() {
        _toolBar = UIToolbar.init(frame: CGRect(x: 0, y: kScreenH - 304, width: kScreenW, height: 44))
        let space = UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let done = UIBarButtonItem.init(title: "确定", style: .done, target: self, action: #selector(doneAction))
        let cancel = UIBarButtonItem.init(title: "取消", style: .done, target: self, action: #selector(cancelAction))
        _toolBar.setItems([cancel, space, done], animated: true)
        
        _pickerView = UIPickerView.init(frame: CGRect(x: 0, y: kScreenH - 260, width: kScreenW, height: 260))
        _pickerView.delegate = self
        _pickerView.dataSource = self
        _pickerView.backgroundColor = UIColor.white
        
        self.addSubview(_pickerView)
        self.addSubview(_toolBar)
        
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(removeView)))
    }
    
    @objc func removeView() {
        var toolBarFrame = _toolBar.frame
        var pickerViewFrame = _pickerView.frame
        pickerViewFrame.origin.y = kScreenH
        toolBarFrame.origin.y = kScreenH
        UIView.animate(withDuration: 0.5, animations: {
            self._toolBar.frame = toolBarFrame
            self._pickerView.frame = pickerViewFrame
        }) { (finished) in
            self._pickerView.removeFromSuperview()
            self._toolBar.removeFromSuperview()
            self.removeFromSuperview()
        }
    }
    
    @objc func cancelAction() {
        self.removeView()
    }
    
    @objc func doneAction() {
        self.removeView()
        let row = self._pickerView.selectedRow(inComponent: 0)
        let text = self._data?[row]
        guard let block = _block else {
            return
        }
        block(text)
    }
}

extension PickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return _data?.count ?? 0
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return _data?[row] ?? nil
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(_data?[row])
    }
}







































